package com.lagou.edu.course.controller;

import com.lagou.edu.common.UserManager;
import com.lagou.edu.common.result.ResponseDTO;
import com.lagou.edu.common.result.ResultCode;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * TODO
 *
 * @author lm
 * @since 2021/10/28 10:38
 */
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseFrontController {
    @Autowired
    CourseRemoteService courseRemoteService;

    @GetMapping("/getAllCourse")
    public ResponseDTO<List<CourseDTO>> getAllCourse(@RequestParam(required = false, name = "userId") Integer userId) {
        return ResponseDTO.success(courseRemoteService.getAllCourses(userId));
    }

    @ApiOperation(value = "获取课程详情")
    @GetMapping(value = "/getCourseById")
    public ResponseDTO<CourseDTO> getCourseById(@RequestParam("courseId") Integer courseId) {
        try {
            Integer userId = UserManager.getUserId();
            CourseDTO courseDTO = this.courseRemoteService.getCourseById(courseId, userId);
            if (courseDTO == null) {
                return ResponseDTO.success();
            }
            return ResponseDTO.success(courseDTO);
        } catch (Exception e) {
            log.error("获取课程详情失败:", e);
            return ResponseDTO.ofError(ResultCode.INTERNAL_ERROR.getState(), ResultCode.INTERNAL_ERROR.getMessage());
        }
    }
}
